//
//  BTMainController.swift
//  BanTang_Swift
//
//  Created by Jacky Sun on 16/8/1.
//  Copyright © 2016年 syf. All rights reserved.
//

import UIKit

class BTMainController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
      
        tabBar.barTintColor = UIColor.whiteColor()
        
        //1、 首页
         let home = BTHomeViewController.init()
          addChildVCByItem(home, title: "首页", image: "home_tab_icon_news")
        

        //2、 广场
        let project = BTProjectViewController.init()
        addChildVCByItem(project, title: "广场", image: "home_tab_icon_market")
       
        
        //3、站位控制器
        let sendMgs = ZhanWeiController.init()
       addChildVCByItem(sendMgs, title: "", image: "")
        
        //4、 消息中心
        let mgsCenter = BTMessageCenterController.init()
        addChildVCByItem(mgsCenter, title: "消息中心", image: "home_tab_icon_selection")
        
        //5、个人中心
        let userCenter = BTHomeViewController.init()
        addChildVCByItem(userCenter, title: "我的", image: "home_tab_icon_mine")
           }

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        // 添加中间点击按钮
        addclickButton()
    }

    
    private func addclickButton(){
        tabBar.addSubview(addBtn)
        let width = UIScreen.mainScreen().bounds.width / CGFloat((viewControllers?.count)!)
        let rect = CGRect(x: 0, y: 0, width: width, height: 46)
        addBtn.frame = CGRectOffset(rect, width * 2, 0);
    }
    
    
    /**
     添加子控制器
     - parameter childVC:
     - parameter title:   title description
     - parameter image:
     */
    private func addChildVCByItem(childVC: UIViewController, title: String, image: String){
        childVC.title = title
        let selImgStr = image + "_selected"
        childVC.tabBarItem = UITabBarItem(title: title, image:UIImage(named: image) , selectedImage: UIImage(named: selImgStr))
        let nav_1 = UINavigationController(rootViewController: childVC)
        addChildViewController(nav_1)
    }
    
    /**
     *  懒加载
     */
    private lazy var addBtn : UIButton = {
        let btn = UIButton(type: UIButtonType.Custom)
            btn.setImage(UIImage(named: "Show_normal"), forState: .Normal)
           btn.adjustsImageWhenHighlighted = false
        btn.addTarget(self, action: #selector(BTMainController.btnClick), forControlEvents: .TouchDown)
        return btn
    }()
    
    
    /**
     点击button
     */
    func btnClick(){
       let xiu = BTProjectViewController.init()
        let nav = UINavigationController(rootViewController: xiu)
        presentViewController(nav, animated: true, completion: nil)
    }
    

}
